//
//  MXHomePageController.m
//  MXUserSys
//
//  Created by 王启颖 on 2018/10/10.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXHomePageController.h"
#import "MXHomeTableView.h"
#import "MXClockView.h"
#import "MXCountView.h"
#import "MXPublishController.h"
#import "MXClockThisWeekModel.h"

#import "ZLDefine.h"

@interface MXHomePageController ()
@property (strong,nonatomic) MXHomeTableView *homeTableView;
@property (strong,nonatomic) MXClockView *clockView;
@property (strong,nonatomic) MXCountView *countView;
@property (strong,nonatomic) UIView *headView;
@property (strong, nonatomic) NSMutableArray *paramsArr;
@property (strong, nonatomic) NSMutableDictionary *dicParams;
@end

@implementation MXHomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"首页";
    
    [self homeTableView];
//    [self headView];
    
    [self getClockThisWeekRequest];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData:) name:@"YZSuccessLoginNotification" object:nil];

}

- (void)refreshData:(NSNotification *)notification{
    [self getClockThisWeekRequest];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}


#pragma mark - 懒加载 -
-(MXHomeTableView *)homeTableView{
    if (!_homeTableView) {
        
        MXHomeTableView *view = [[MXHomeTableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarViewHeight - KSafeAreaBottonHeight - KSafeAreaTopHeight)];
        _homeTableView = view;
        _homeTableView.navigationController = self.navigationController;
        _homeTableView.tableView.tableHeaderView = self.headView;
        [self.view addSubview:_homeTableView];
    }
    return _homeTableView;
}

-(MXClockView *)clockView{
    if (!_clockView) {
        
        MXClockView *view = [MXClockView initMXClockView];
        view.frame = CGRectMake(0,0, kScreenWidth, 160);
        view.clockBlock = ^{
            ZLOnePhoto *one = [ZLOnePhoto shareInstance];
            [one presentPicker:PickerType_Camera photoCut:PhotoCutType_NO target:self callBackBlock:^(UIImage *image, BOOL isCancel) {
                MXPublishController *vc = [[MXPublishController alloc]init];
//                vc.lastSelectAssets = assets.mutableCopy;
                
                vc.publishImage = image;
                [self.navigationController pushViewController:vc animated:YES];
            }];
        };
        _clockView = view;
        
    }
    return _clockView;
}



-(MXCountView *)countView{
    if (!_countView) {
        
        MXCountView *view = [MXCountView initMXCountView];
        view.frame = CGRectMake(0,self.clockView.height, kScreenWidth, 125);
        _countView = view;
        
    }
    return _countView;
}

-(UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 285)];
        [_headView addSubview:self.clockView];
        [_headView addSubview:self.countView];
    }
    return _headView;
}


- (NSMutableDictionary *)dicParams {
    if (!_dicParams) {
        _dicParams = [NSMutableDictionary dictionary];
    }
    [self initdicParams];
    return _dicParams;
}
- (void)initdicParams {
    _dicParams[@"user_id"] = [MXUserMessageTool message].user_id;
    
}
-(NSMutableArray *)paramsArr{
    if (!_paramsArr) {
        _paramsArr = [[NSMutableArray alloc]init];
    }
    return _paramsArr;
}

#pragma mark - 网络请求 -
-(void)getClockThisWeekRequest{
    [SVProgressHUD showWithStatus:@"正在检测打卡。。。"];

    [[MXHttpManagerClass HttpRequstManager] PostURL:getClockThisWeek params:self.dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [SVProgressHUD dismiss];

        if (code == 0) {
            NSLog(@"%@",result);

            for (id res in result[@"message"]) {
                MXClockThisWeekModel *model = [MXClockThisWeekModel mj_objectWithKeyValues:res];
                NSInteger dateWeek = [NSString dateWeekWithDateString:[NSString stringWithFormat:@"%ld",model.created_at/1000]];


                [self.paramsArr addObject:[NSNumber numberWithInteger:dateWeek]];
            }

            self.clockView.isClock = [self isClock];
            self.countView.paramsArr = self.paramsArr;

        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:result[@"message"]];
        }

    } fail:^(NSError *error) {

        NSLog(@"%@",error);
    }];

}

-(BOOL)isClock{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];

    NSInteger today = [NSString dateWeekWithDateString:timeSp];

    for (NSNumber *time in self.paramsArr) {
        if ( [time integerValue] == today) {
            return true;
        }
    }

    return false;

}



@end
